<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2018/12/19
 * Time: 21:14
 */
namespace common\sources\read\businessrechargerecord;

use common\models\craftsman\BusinessRechargeRecord;

class BusinessRechargeRecordAccess implements IBusinessRechargeRecordAccess
{
    public function checkRecharge($data) {
        $check = BusinessRechargeRecord::find()
            ->select('amount,bus_id,order_id,status')
            ->where(['bus_id' => $data['id'],'order_id' => $data['paycode'],'status' => 2])
            ->asArray()
            ->one();
        return $check;
    }

    public function RechargeList($data,$count = false) {
        $query = BusinessRechargeRecord::find()
            ->alias('b')
            ->select("b.bus_id,b.order_id,b.amount,FROM_UNIXTIME(b.create_time) as create_time,b.status,b.rech_type,b.pay_type,
            u.phone,u.role")
            ->leftJoin("buyuser u",'u.id = b.bus_id')
            ->where(['rech_type' => $data['change'],'status' => 2]);
        if (!empty($data['phone'])) {
            $query = $query->andWhere(['u.phone' => $data['phone']]);
        }
        if (!empty($data['order_id'])) {
            $query = $query->andWhere(['b.order_id' => $data['order_id']]);
        }
        if (!empty($data['begin_time'])) {
            $query = $query->andWhere([
                '>',
                'create_time',
                $data['begin_time']
            ]);
        }

        $orderBy = ' create_time  '.$data['sort_type'];
        if (!empty($data['end_time'])) {
            $query = $query->andWhere([
                '<',
                'create_time',
                $data['end_time']
            ]);
        }
        if ($count) {
            $count = $query->asArray()->count();
            return $count;
        }

        $list = $query->offset(($data['page']-1)*$data['limit'])
            ->limit($data['limit'])
	        ->orderBy($orderBy)
            ->asArray()
            ->all();
        return $list;
    }
}